home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 619 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.4 KB  |  177 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: Valentin Bonnard <bonnardv@pratique.fr>
  3. Newsgroups: comp.std.c++
  4. Subject: Some ideas about C++ and a question
  5. Date: 04 Mar 1996 10:21:57 PST
  6. Organization: Internet Way
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4h9se5$m2n@s3.iway.fr>
  9. NNTP-Posting-Host: isolde.mti.sgi.com
  10. X-Original-Date: 2 Mar 1996 16:19:17 GMT
  11. X-Mailer: Mozilla 1.1N (Macintosh; I; 68K)
  12. X-Url: news:comp.std.c++#4h4oiv$4md@xanadu.io.com
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMTs04ky4NqrwXLNJAQEyIAH/eVr7vMtLcVaUGIMEbDSpc3KZSqZJu6IR
  15.     x6+tgRRo9VKZYU81f8CNSY/7HbM/8vL5sVt2Nhsb2biWFsWd8JqK4w==
  16.     =zNg4
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. I have 4 ideas about C++ and a question.
  20.  
  21. 1) String literals
  22. ~~~~~~~~~~~~~~~~~~
  23. String literals should be const char* instead of char* (if it is not
  24. already the case).
  25.  
  26.  
  27. 2) Placement operators
  28. ~~~~~~~~~~~~~~~~~~~~~~
  29. class Object {
  30.         void    operator+ // or / or whatever
  31.                          (Object& result, const Object& b) const;
  32.         void    operator* (Object& result) const;
  33.         Object  operator* (const Object& result) const; // old one
  34. }
  35.  
  36. should be called respectively by:
  37. a = b + c;
  38.  
  39. and a = *b;
  40.  
  41. and (old one) a*b;
  42.  
  43. This will solve the [well-known] problem of copying the result into a temporary.
  44. The compatibility will be preserved since old syntax will be allowed to.
  45.  
  46. 3) Use of explicit keyword
  47. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. //========= Test.h =========
  49. class Base {
  50.     public:
  51.         virtual void VirtualFunc ();
  52. };
  53.  
  54. class Derived : public Base {
  55.     public:
  56.         void VirtualFunc () { }
  57. };
  58.  
  59. //========= Caller.cp =========
  60. #include <Test.h>
  61. class Derived2 : public Derived {
  62.     public:
  63.         void VirtualFunc ();
  64. };
  65.  
  66. //========= Foo.cp =========
  67. #include <Test.h>
  68.  
  69. void    foo (explicit Derived* object)
  70. {
  71.     object.VirtualFunc (); // nop (compiler does not generate code for this)
  72.  
  73.     Derived* AnyDerivedObject = object; // ok
  74. }
  75.  
  76. void    foo2 (Derived* object)
  77. {
  78.     object.VirtualFunc (); // object can be Derived2
  79.  
  80.     explicit Derived* RealDerivedObject = object; // error
  81. }
  82.  
  83. The meaning of explicit could be: while a derived class ptr [resp. ref] can be
  84. convert to a base class ptr [ref], it can't be converted to a base class 
  85. explicit ptr [ref].
  86.  
  87. So explicit would be a type qualifier; this usage is different with:
  88.  
  89. > Rich Hickey
  90. > rich@rcs-hq.mhs.compuserve.com
  91.  
  92. > I propose that the explicit keyword be allowed as a qualifier of a class
  93. > definition, such qualification taking the form of:
  94.  
  95. > explicit class X{
  96. >   //the definition of X
  97. >   };
  98.  
  99. Here, X cannot be derived but my Derived can be so I think my idea is better
  100. (please fell free comment/criticise this assertion).
  101.  
  102.  
  103. 4) Conversion of Type** to const Type**
  104. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105.  
  106. It's ok, this is an error; but why cannot Type** be converted to const Type* 
  107. const* ?
  108.  
  109. void    foo ()
  110. {
  111.     typedef int Type;
  112.     Type        i;
  113.     const Type  ci = 2;
  114.     Type*       pi = &i;
  115.     Type**      ppi = π
  116.  
  117.     const Type** ppci = ppi;            // error because
  118.     *ppci = &ci;                        // now pi == &ci
  119.     *pi = 0;                            // and now we modify ci
  120.  
  121.     const Type*const* pcpci = ppi;      // no problem because
  122.     *pcpci = &ci;                       // this is impossible (since *pcpci is 
  123. const)
  124. }
  125.  
  126.  
  127. Now a question:
  128.  
  129. Will there be any way to redefine (overload) normal && or || on class ?
  130. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  131.  
  132. I mean, now:
  133.  
  134. class Int {
  135.     public:
  136.         operator int () const { return i; }
  137.         operator && (int) const;
  138.         operator || (int) const;
  139.         Int (int I) : i(I) { }
  140.  
  141.     private:
  142.         int     i;
  143. };
  144.  
  145. int     Func ()
  146. {
  147.     cout << "called\n";
  148.     return 1;
  149. }
  150.  
  151. void    foo ()
  152. {
  153.     int i (1);
  154.     Int I (1);
  155.  
  156.     if (i || Func ()) // called is not printed
  157.         ;
  158.     if (I || Func ()) // called is printed
  159.         ;
  160. }
  161.  
  162. One could think that the standard should be change in order to support real
  163. overloaded operator && and ||.
  164.  
  165. I can't see the point of doing that, so if it's the case for you to don't ask
  166. me nor criticise.
  167.  
  168. Valentin Bonnard
  169. bonnardv@pratique.fr
  170. ---
  171. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  172.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  173.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  174.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  175.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  176. ]
  177.